import random
import requests
import numpy as np
import feffery_antd_charts as fact
import feffery_antd_components as fac
import feffery_leaflet_components as flc

data = (
    requests
    .get('https://geo.datav.aliyun.com/areas_v3/bound/100000_full.json')
    .json()
)

# 构建随机示例属性字段
for i in range(len(data['features'])):
    data['features'][i]['properties'] = {
        **data['features'][i]['properties'],
        'random_value': round(random.uniform(0, 1), 3)
    }

    data['features'][i]['properties']['tooltip'] = '{}，示例数值：{}'.format(
        data['features'][i]['properties']['name'],
        data['features'][i]['properties']['random_value']
    )

rose_chart_demo = fact.AntdRose(
    data=[
        {
            'type': '分类一',
            'value': 27,
        },
        {
            'type': '分类二',
            'value': 25,
        },
        {
            'type': '分类三',
            'value': 18,
        },
        {
            'type': '分类四',
            'value': 15,
        },
        {
            'type': '分类五',
            'value': 10,
        },
        {
            'type': '其他',
            'value': 5,
        },
    ],
    xField='type',
    yField='value',
    seriesField='type',
    radius=0.9,
    innerRadius=0.4,
    legend={
        'position': 'bottom',
    },
    autoFit=True
)

pie_chart_demo = fact.AntdPie(
    data=[
        {
            'type': '分类一',
            'value': 27,
        },
        {
            'type': '分类二',
            'value': 25,
        },
        {
            'type': '分类三',
            'value': 18,
        },
        {
            'type': '分类四',
            'value': 15,
        },
        {
            'type': '分类五',
            'value': 10,
        },
        {
            'type': '其他',
            'value': 5,
        },
    ],
    angleField='value',
    colorField='type',
    radius=0.9,
    label={
        'type': 'inner',
        'offset': '-30%',
        'style': {
            'fontSize': 16,
            'textAlign': 'center'
        },
        'formatter': {
            'func': '({ percent }) => `${(percent * 100).toFixed(0)}%`'
        }
    }
)

map_demo = flc.LeafletMap(
    [
        flc.LeafletTileLayer(
            url='http://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=8&x={x}&y={y}&z={z}'
        ),

        flc.LeafletGeoJSON(
            data=data,
            mode='choropleth',
            featureValueField='random_value',
            showTooltip=True,
            featureValueToStyles={
                'bins': [
                    [0, 0.33],
                    [0.33, 0.66],
                    [0.66, 1]
                ],
                'styles': [
                    {
                        'fillColor': '#ffccc7',
                        'fillOpacity': 1,
                        'weight': 1,
                        'color': 'white'
                    },
                    {
                        'fillColor': '#ff7875',
                        'fillOpacity': 1,
                        'weight': 1,
                        'color': 'white'
                    },
                    {
                        'fillColor': '#f5222d',
                        'fillOpacity': 1,
                        'weight': 1,
                        'color': 'white'
                    }
                ]
            },
            fitBounds=False
        )
    ],
    zoom=3,
    center={
        'lng': 106,
        'lat': 37
    },
    viewAutoCorrection=True,
    style={
        'height': '100%'
    }
)


table_demo = fac.AntdTable(
    columns=[
        {
            'title': '数值测试1',
            'dataIndex': '数值测试1',
            'width': '50%',
            'renderOptions': {
                'renderType': 'custom-format'
            }
        },
        {
            'title': '数值测试2',
            'dataIndex': '数值测试2',
            'width': '50%',
            'renderOptions': {
                'renderType': 'custom-format'
            }
        }
    ],
    data=[
        {
            '数值测试1': np.random.rand(),
            '数值测试2': np.random.rand()
        }
        for i in range(5)
    ],
    sortOptions={
        'sortDataIndexes': ['数值测试1', '数值测试2']
    },
    bordered=True,
    customFormatFuncs={
        '数值测试1': '(x) => `${(x*100).toFixed(2)}%`',
        '数值测试2': '(x) => x <= 0.5 ? `低水平：${x.toFixed(2)}` : `高水平：${x.toFixed(2)}`',
    },
    style={
        'width': '100%'
    }
)
